<?php
namespace app\common\model;
use think\Cookie;

class AdminToken extends Base
{
    /**
     * 设置永久登录
     * @param string $phone
     * @param string $uid
     * @return boolean
     */
    public function setLongLogin($phone,$uid)
    {
        $salt = random_str(16); // 16位随机码
        $identifier = md5($salt . md5($phone . $salt)); //第二分身标识
        $token = md5(uniqid(rand(), true)); // 永久登录标识
        $timeout = time()+604800; // 永久登录超时时间(1周)
        Cookie::set('auth',"$identifier:$token",['expire'=>604800,'prefix'=>'think_']); // 存入cookie
        return $this->saveRemember($uid,$identifier,$token,$timeout); // token入库
    }

    /**
     * 保存token
     * @param string $uid
     * @param string $identifier
     * @param string $token
     * @param string $timeout
     * @return boolean
     */
    public function saveRemember($uid,$identifier,$token,$timeout)
    {
        $data['identifier'] = $identifier;
        $data['token'] = $token;
        $data['timeout'] = $timeout;
        $data['uid'] = $uid;
        $userstoken=$this->where(array('uid'=>$uid))->find();
        // 判断有无数据, 有则修改, 无则添加
        if($userstoken){
            $save = $this->allowField(true)->save($data,[ 'uid' => $data['uid']]);
        }else{
            $save  = $this->allowField(true)->save($data);
        }
        if ( $save == 0 || $save == false) {
            return false;
        }else{
            return true;
        }
    }


    /**
     * 操作token信息
     * @param string $type checkRemember-检查是否永久(1周)登录 或 deltoken-退出后删除token
     * @return boolean
     */
    public function editToken($type)
    {
        if(!Cookie::has('auth','think_')) return false;
        $arr = array();
        $now = time();
        list($identifier,$token) = explode(':',Cookie::get('auth','think_'));
        if (ctype_alnum($identifier) && ctype_alnum($token)){  // ctype_alnum — 做字母和数字字符检测
            $arr['identifier'] = $identifier;
            $arr['token'] = $token;
        }else{
            return false;
        }

        $tokenInfo = $this->getArrayByMap(['identifier'=>$arr['identifier']],'','',false);  // 查询token信息
        if(empty($tokenInfo)) return false;
        $info = model("admin")->getArrayByMap(['id'=>$tokenInfo['uid']],'','',true);  // 查询用户信息

        if($info){
            if($type == 'checkRemember'){  // 检查是否永久(1周)登录
                if($arr['token'] != $tokenInfo['token']){  // 校对token
                    return false;
                }else if($now > $tokenInfo['timeout']){  // 校对有效期
                    $this->where(array('uid'=>$tokenInfo['uid']))->delete();
                    return false;
                }else{
                    return $info;
                }
            }else if($type == 'deltoken'){  // 退出后删除token
                if($arr['token'] != $tokenInfo['token']){
                    return false;
                }else{
                    $res = $this->where(array('uid'=>$tokenInfo['uid']))->delete();
                    if($res){
                        return true;
                    }else{
                        return false;
                    }
                }
            }
        }else{
            return false;
        }
    }

}